package com.cupk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cupk.mapper.StudentMapper;
import com.cupk.pojo.Student;
import com.cupk.service.StudentService;
import com.cupk.utils.HttpUtil;
import com.cupk.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Map<String, Object> exchangeVouchers(String requestStr) {
        String out = HttpUtil.get(requestStr);
        JSONObject userInfo = JSONObject.parseObject(out);
        // 构造返回类型
        Map<String, Object> map = new HashMap<>();
        map.put("openid", userInfo.getString("openid"));
        map.put("unionid", userInfo.getString("unionid"));
        map.put("code", userInfo.getString("errcode"));
        return map;
    }

    @Override
    public String generateToken(Student student) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", student.getId());
        claims.put("name", student.getName());
        claims.put("stuId", student.getStuId());
        claims.put("identity_card", student.getIdentityCard());
        claims.put("status", student.getStatus());
        claims.put("grade", student.getGrade());
        claims.put("college", student.getCollege());
        claims.put("major", student.getMajor());
        claims.put("class", student.getClasses());
        String token = JwtUtil.genToken(claims);
        return token;
    }

    @Override
    public Student findStudentByIdentity(String stuId, String identityCard) {
        return studentMapper.findStudentByIdentity(stuId, identityCard);
    }

    @Override
    public void bindingStudent(String stuId, String openid, String unionid, String phone) {
        studentMapper.addAccount(stuId, openid, unionid);
        studentMapper.updateMobile(stuId,phone);
    }

    @Override
    public Student findStudentByStuId(String stuId) {
        return studentMapper.findStudentByStuId(stuId);
    }

    @Override
    public Student findStudentById(Integer id) {
        return studentMapper.findStudentById(id);
    }


    @Override
    public String getAccessToken(String appid, String appSecret) {
        String requestStr = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appSecret;
        String out = HttpUtil.get(requestStr);
        JSONObject info = JSONObject.parseObject(out);
        String accessToken = info.getString("access_token");
        return accessToken;
    }

    @Override
    public Map<String, Object> getRealMobile(String appSecret, String code) {
        String requestStr = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + appSecret;
        Map<String, String> body = new HashMap<>();
        body.put("code", code);
        String out = HttpUtil.post(requestStr, body);
        JSONObject info = JSONObject.parseObject(out);
        return info;
    }

    @Override
    public List<Student> studentList(String query) {
        return studentMapper.searchStudents(query);
    }

    @Override
    public void updatePhone(String realPhone, String stuId) {
        studentMapper.updateMobile(stuId,realPhone);
    }

    @Override
    public String findOpenIdByStuId(String stuId) {
        return studentMapper.findOpenIdByStuId(stuId);
    }

    @Override
    public void updateAvatars(String stuId, String url) {
        studentMapper.updateAvatars(stuId,url);
    }

    @Override
    public void setBlackListStatus(Integer stuId, Integer status) {
        studentMapper.setBlackListStatus(stuId,status);
    }


    @Override
    public Student findStudentByOpenid(String openid) {
        return studentMapper.findStudentByOpenid(openid);
    }


}
